package cc.customer.index;

import cc.customer.expCustomer.dao.ExEmpMapper;
import cc.customer.expCustomer.domain.ExEmp;
import cc.customer.bigEmport.damain.BigEmp;
import cc.customer.bigEmport.dao.BigEmpMapper;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.DateUtil;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.dao.CusResourceMapper;
import cc.customer.myCustomer.dao.MyCustomerMapper;
import cc.customer.onschool.dao.OnSchoolResourceMapper;
import cc.customer.onschool.domain.OnSchoolResource;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.text.DecimalFormat;
import java.util.Arrays;


@Service
@Transactional(propagation = Propagation.REQUIRED,readOnly = true,rollbackFor = Exception.class)
public class IndexServicesImpl implements IndexServices{

    @Autowired
    private BigEmpMapper bigEmpMapper; //大资源

    @Autowired
    private ExEmpMapper exEmpMapper; //推广资源

    @Autowired
    private OnSchoolResourceMapper onSchoolResourceMapper; //网校资源

    @Autowired
    private MyCustomerMapper myCustomerMapper; //个人资源

    @Autowired
    private CusResourceMapper cusResourceMapper; //四类资源 统计


    @Override
    @Transactional
    public ResponseBo getIndexNumStatis() {

        JSONObject object = new JSONObject();
        try {
            //查询所有得 大资源客户
            BigEmp bigEmp = new BigEmp();
            bigEmp.setBpIsdelete("0");
            int allBigEmp = bigEmpMapper.selectCount(bigEmp);
            object.put("allBigEmp",allBigEmp);

            //查询所有得 意向用户
            Example example = new Example(BigEmp.class);
            example.createCriteria().andIn("bpGroud", Arrays.asList(Constant.YIXIANG_GROUP_ALL));
            int AllYiGroup = bigEmpMapper.selectCountByExample(example);
            object.put("AllYiGroup",AllYiGroup);

            //查询所有得 推广资源
            ExEmp exEmp =  new ExEmp();
            exEmp.setcExIsdelete("0");
            int AllExEmp = exEmpMapper.selectCount(exEmp);
            object.put("AllExEmp",AllExEmp);

            //查询所有得网校资源
            OnSchoolResource onSchoolResource = new OnSchoolResource();
            onSchoolResource.setOsDealStatus("0");
            int AllSchool = onSchoolResourceMapper.selectCount(onSchoolResource);
            object.put("AllSchool",AllSchool);

            //查询 所有得 个人资源
            MyCustomer myCustomer = new MyCustomer();
            myCustomer.setMyCusIsdelete("0");
            int AllMyCustomer = myCustomerMapper.selectCount(myCustomer);
            object.put("AllMyCustomer",AllMyCustomer);

            //查询公海得数据
            CusResource cusResource = new CusResource();
            cusResource.setDelstatus("2");
            int AllGongSea = cusResourceMapper.selectCount(cusResource);
            object.put("AllGongSea",AllGongSea);

            /**各个占比**/
            int allZiyuan = allBigEmp+AllExEmp+AllSchool+AllMyCustomer;
            DecimalFormat df = new DecimalFormat("######0.00");

            //大资源占比
            double bigCout = ((double)allBigEmp/allZiyuan)*100;
            object.put("bigCout",df.format(bigCout));
            //推广资源占比
            double extCout = ((double)AllExEmp/allZiyuan)*100;
            object.put("extCout",df.format(extCout));
            //网校资源占比
            double schoolCout = (((double)AllSchool)/allZiyuan)*100;
            object.put("schoolCout",df.format(schoolCout));
            //个人新增资源占比
            double myCout = (((double)AllMyCustomer)/allZiyuan)*100;
            object.put("myCout",df.format(myCout));

            //大资源意向占比
            double YiXiangCout = (((double)AllYiGroup)/allZiyuan)*100;
            object.put("YiXiangCout",df.format(YiXiangCout));
            //公海资源占比
            double GongSeaCout = ((double)AllGongSea/allZiyuan)*100;
            object.put("GongSeaCout",df.format(GongSeaCout));
            return ResponseBo.ok(object);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error();
    }

    @Override
    public ResponseBo getIndexMintorStatis() {
        JSONObject object = new JSONObject();
        String today = DateUtil.getCurrentDate("yyyy-MM-dd");
        try {
            //查询所有得 今日新增 大资源客户
            Example example = new Example(BigEmp.class);
            example.createCriteria().andLike("bpCreatetime", today+"%");
            int AllBigToDay = bigEmpMapper.selectCountByExample(example);
            object.put("AllBigToDay",AllBigToDay);

            //查询所有得 今日新增 推广资源
            Example example2 = new Example(ExEmp.class);
            example2.createCriteria().andLike("cExTime", today+"%");
            int AllExToDay = exEmpMapper.selectCountByExample(example2);
            object.put("AllExToDay",AllExToDay);

            //查询所有得网校资源
            Example example3 = new Example(OnSchoolResource.class);
            example3.createCriteria().andLike("osImportTime", today+"%");
            int AllSchToday = onSchoolResourceMapper.selectCountByExample(example3);
            object.put("AllSchToday",AllSchToday);

            //查询 所有得 个人资源
            Example example4 = new Example(MyCustomer.class);
            example4.createCriteria().andLike("myCusCreatetime", today+"%");
            int AllMyCusday = myCustomerMapper.selectCountByExample(example4);
            object.put("AllMyCusday",AllMyCusday);

            return ResponseBo.ok(object);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error();
    }


}
